package game;

import images.ImageLoader;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.util.LinkedList;

import map.*;
import math.Vector2D;

import gamestate.StateStack;
import gui.user.User;

public class InGame extends gamestate.State
{
	private ImageLoader wallCorner;
	
	private Polygon cursor;
	
	private PlayingField pf;
	
	private PathFinder pathFinder;
	private Vector2D start, end;
	private LinkedList<Vector2D> path;
	
	public InGame( StateStack s, User u ) 
	{
		super( s, u );

		wallCorner = new ImageLoader( "Images/wallCorner.png" );
		
		pf = new PlayingField( 1024 / 32, 768 / 8 );
		
		pathFinder = new PathFinder( pf );
		start = end = null;
		path = null;
		
		cursor = new Polygon();
		cursor.addPoint( 16, 0 );
		cursor.addPoint( 32, 8 );
		cursor.addPoint( 16, 15 );
		cursor.addPoint( 0, 8 );
	}

	@Override
	public void render( Graphics2D g ) 
	{
		pf.render( g );
		
		Vector2D mouse = PlayingField.fromWorldToWorldOnGrid( getUser().getMouseMove() );
		g.setTransform( AffineTransform.getTranslateInstance( mouse.getX() - cursor.getBounds().getWidth() / 2, mouse.getY() - cursor.getBounds().getHeight() / 2 ) );
		g.setColor( new Color( 255, 255, 255, 128 ) );
		g.fillPolygon( cursor );
		g.setTransform( new AffineTransform() );
		g.setColor( Color.BLACK );
		g.drawOval( (int)mouse.getX() - 1, (int)mouse.getY() - 1, 2, 2 );
		
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 21, 			5 * 8 ) );
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 21 - 16, 	6 * 8 ) );
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 20, 			7 * 8 ) );
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 20 - 16, 	8 * 8 ) );
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 19, 			9 * 8 ) );
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 19 - 16, 	10 * 8 ) );
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 18, 			11 * 8 ) );
		wallCorner.draw( g, AffineTransform.getTranslateInstance( 32 * 18 - 16, 	12 * 8 ) );
		
		if( path != null )
			for( int i = 1; i < path.size(); i++ )
			{
				Vector2D v = path.get( i - 1 );
				Vector2D v2 = path.get( i );
			
				g.setColor( Color.blue );
				g.drawOval( (int)v.getX() - 1, (int)v.getY() - 1, 2, 2 );
				g.drawOval( (int)v2.getX() - 1, (int)v2.getY() - 1, 2, 2 );
			
				g.setColor( Color.black );
				g.drawLine( (int)v.getX(), (int)v.getY(), (int)v2.getX(), (int)v2.getY() );
			}
	}

	@Override
	public void update( double duration ) 
	{
		Vector2D mouse = getUser().getMousePos();
		if( mouse != null )
		{
			mouse = PlayingField.fromWorldToGrid( mouse );
			
			if( getUser().getButton() == 1 )
			{
				pf.getTile( mouse ).setOccupied( !pf.getTile( mouse ).isOccupied() );
			
				if( start != null && end != null )
					path = pathFinder.findPath( start, end );
			}
			else if( getUser().getButton() == 3 )
			{
				if( start == null )
				{
					if( pf.isValid( mouse ) )
						start = pf.getTile( mouse ).getLocation();
				}
				else if( end == null )
				{
					if( pf.isValid( mouse ) )
					{
						end = pf.getTile( mouse ).getLocation();
					
						path = pathFinder.findPath( start, end );
					}
				}
				else
				{
					start = end = null;
					path = null;
				}
			}
			
			getUser().doneWithMouse();
		}
	}
	
}
